package cn.xaut.动态规划;

/**
 * 152. 乘积最大子数组
 */
public class demo152 {

    public int maxProduct(int[] nums) {

        int res = Integer.MIN_VALUE, imax = 1, imin = 1;
        for (int num : nums) {
            if (num < 0) {
                int temp = imax;
                imax = imin;
                imin = temp;
            }
            imax = Math.max(num, num * imax);
            imin = Math.min(num, num * imin);
            res = Math.max(res, imax);
        }
        return res;
    }

    public static void main(String[] args) {

        System.out.println(new demo152().maxProduct(new int[]{2, 3, -2, 4}));   // 6
        System.out.println(new demo152().maxProduct(new int[]{-2, 0, -1}));   // 0
    }
}
